﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Win.Forms
{
    public partial class ImportWizardForm : BaseForm
    {
        public ImportWizardForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 下一步事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void wizardControl1_NextClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            e.Handled = true;
            switch (wizardControl1.SelectedPage.Name)
            {
                case "welcomeWizardPage1":
                    wizardControl1.SelectedPage = wpElsImport;
                    break;
                //文件导入
                case "wpElsImport":
                    //加载文件，解析数据
                    var view = ParseImport(editElsImport.Text);
                    //错误进入错误页面
                    if (view == null)
                    {
                        wizardControl1.SelectedPage = wpImportError;
                        return;
                    }
                    //正确进入上传页面
                    wpUpload.Tag = view;
                    wizardControl1.SelectedPage = wpUpload;
                    break;
                //上传页面
                case "wpUpload":

                    ViewModel["ImportType"] = radioGroup1.EditValue;
                    var exec = ViewProvider.ExecuteUpdate(ViewModel);
                    //成功
                    if (exec)
                    {
                        wizardControl1.SelectedPage = completionWizardPage1;
                        return;
                    }

                    editImportError.Text = string.Format("失败原因:\r\n {0} \r\n 点击[上一步]重新导入 \r\n 点击[取消]结束操作", Message);
                    wizardControl1.SelectedPage = wpImportError;
                    break;
            }

        }

        Fis.Win.Utils.IDataTrans ParseImport(string file)
        {
            var transType = ViewModel["TransType"].ToString();
            Fis.Win.Utils.IDataTrans view = null;
            switch (transType)
            {
                case "0":
                    System.Collections.Hashtable bill = new System.Collections.Hashtable();
                    bill["filter0"] = " AND FSTATUS=4";
                    bill["filter1"] = string.Empty;
                    bill["listtype"] = "init";
                    view = Fis.Win.Utils.AppSession.GetViewProvider("BillListViewProvider").CreateView<BillListForm>(Agile.Engine.View.ViewState.View, bill);
                    break;
                case "1":
                    view = Fis.Win.Utils.AppSession.GetViewProvider("AccountListViewProvider").CreateView<AccountListForm>(Agile.Engine.View.ViewState.View, null);
                    break;
                case "2":
                    var aids = new System.Collections.Hashtable();
                    aids["key"] = "1";
                    aids["showAll"] = "1";
                    view = Fis.Win.Utils.AppSession.GetViewProvider("AidsListViewProvider").CreateView<AidsListForm>(Agile.Engine.View.ViewState.View, aids);
                    break;
                //币种
                case "3":
                    view = Fis.Win.Utils.AppSession.GetViewProvider("RateListViewProvider").CreateView<RateListForm>(Agile.Engine.View.ViewState.View, null);
                    break;
            }

            var msg = view.FromXls(file);

            var errFormat = "失败原因:\r\n {0} \r\n 点击[上一步]重新导入 \r\n 点击[取消]结束操作";
            var data = (view as Agile.Engine.View.IView).ViewModel["viewData"] as DataTable;
            //成功,返回View
            if (string.IsNullOrEmpty(msg))
            {
                if (data.Rows.Count > 0)
                {
                    ViewModel["data"] = data;
                    return view;
                }
                else
                {
                    //失败，设置错误信息，返回空
                    editImportError.Text = string.Format(errFormat, "没有一行有效的非系统数据可以导入");
                }
            }
            else
            {
                //失败，设置错误信息，返回空
                editImportError.Text = string.Format(errFormat, msg);
            }

            return null;
        }


        private void editElsImport_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                dlg.Filter = "Excel 文件 (*.xls) | *.xls";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    editElsImport.Text = dlg.FileName;
                    wpElsImport.AllowNext = true;
                }
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (radioGroup1.EditValue.ToString())
            {
                case "1":
                    editSaveRemarks.Text = "保存前将删除所有手工数据，仅保留系统数据和导入的新数据，请确认新数据中的完整性";
                    break;
                case "2":
                    editSaveRemarks.Text = "保留所有手工数据并导入新的数据，有可能导致数据重复和保存失败";
                    break;

            }
        }

        private void bnView_Click(object sender, EventArgs e)
        {
            var view = wpUpload.Tag;
            if (view is ListForm)
            {
                (view as ListForm).ShowBarManager = false;
            }
            else if (view is BillListForm)
            {
                (view as BillListForm).ShowBarManager = false;
            }
            (view as Form).ShowDialog();
        }

        private void wizardControl1_PrevClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            e.Handled = true;
            switch (wizardControl1.SelectedPage.Name)
            {
                //文件导入
                case "wpImportError":
                    wizardControl1.SelectedPage = wpElsImport;
                    break;
                //上传页面
                case "wpUpload":
                    wizardControl1.SelectedPage = wpElsImport;
                    break;
            }
        }

        private void ImportWizardForm_Load(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder("导入说明:\r\n");
            switch (ViewModel["TransType"].ToString())
            {
                case "0":
                    Text = "初始凭证导入向导";
                    builder.AppendLine("导入模板请在初始数据管理中导出");
                    builder.AppendLine("Excel中对应的数据表名称必须是凭证列表");
                    builder.AppendLine("顺序号和会计科目同时为空的行不会被导入");
                    builder.AppendLine("顺序号不能为空");
                    builder.AppendLine("凭证类型不能为空");
                    builder.AppendLine("凭证类型必须与帐套信息中的定义匹配");
                    builder.AppendLine("制表日期不能为空");
                    builder.AppendLine("摘要不能为空");
                    builder.AppendLine("会计科目不能为空");
                    builder.AppendLine("会计科目不是最末级科目导入后需要重新输入");
                    builder.AppendLine("其他辅助字段不会被导入");
                    break;
                case "1":
                    Text = "会计科目导入向导";
                    builder.AppendLine("导入模板请在科目列表中导出");
                    builder.AppendLine("Excel中对应的数据表名称必须是会计科目");
                    builder.AppendLine("固定会计科目会被导入");
                    builder.AppendLine("科目编码或科目名称为空的行不会被导入");
                    builder.AppendLine("科目类别不能为空");
                    builder.AppendLine("余额方向不能为空");
                    builder.AppendLine("科目编码不能重复定义");
                    builder.AppendLine(string.Format("科目编码必须匹配{0}规则", Fis.Win.Utils.AppSession.Book["FACCSTRUCT"]));

                    break;
                case "2":
                    Text = "辅助核算导入向导";
                    builder.AppendLine("导入模板请在辅助核算列表中导出");
                    builder.AppendLine("Excel中对应的数据表名称必须是辅助核算");
                    builder.AppendLine("编码不能为空");
                    builder.AppendLine("编码不能重复定义");
                    builder.AppendLine("类别不能为空");
                    builder.AppendLine("类别必须对应导出时的类别名称");
                    builder.AppendLine("不会导入是否默认属性，需要手工设置");
                    break;
                case "3":
                    Text = "币种汇率导入向导";
                    builder.AppendLine("导入模板请在币种汇率列表中导出");
                    builder.AppendLine("Excel中对应的数据表名称必须是币种汇率");
                    builder.AppendLine("本位币不会被导入");
                    builder.AppendLine("币种编码不能为空");
                    builder.AppendLine("币种编码不能重复定义");
                    break;
            }

            builder.AppendLine();
            builder.AppendLine("点击[下一步]继续操作");
            editRemark.Text = builder.ToString();
        }



        bool isCancelClose = true;
        private void wizardControl1_CancelClick(object sender, CancelEventArgs e)
        {
            if (DialogResult.Yes == DevExpress.XtraEditors.XtraMessageBox.Show(
                "是否关闭导入向导?", "", MessageBoxButtons.YesNo
                ))
            {
                isCancelClose = false;
            }
        }

        private void ImportWizardForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (wizardControl1.SelectedPage == completionWizardPage1)
                return;

            e.Cancel = isCancelClose;
            isCancelClose = true;
        }
    }
}
